package com.suli.multimedia.service.serviceImpl;

import com.aliyun.oss.internal.OSSUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suli.multimedia.entity.Image;
import com.suli.multimedia.entity.Scenic;
import com.suli.multimedia.entity.ScenicTicket;
import com.suli.multimedia.entity.vo.ScenicVO;
import com.suli.multimedia.enums.ScenicType;
import com.suli.multimedia.mapper.ImageMapper;
import com.suli.multimedia.mapper.ScenicMapper;
import com.suli.multimedia.mapper.ScenicTicketMapper;
import com.suli.multimedia.service.ScenicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suli.multimedia.util.OssUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.DateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SuLi
 * @since 2022-03-29
 */
@Service
@Transactional
public class ScenicServiceImpl extends ServiceImpl<ScenicMapper, Scenic> implements ScenicService {

    @Autowired
    ScenicMapper scenicMapper;
    @Autowired
    ImageMapper imageMapper;
    @Autowired
    OssUtil ossUtil;
    @Autowired
    ScenicTicketMapper ticketMapper;

    @Override
    public Map<String, Object> getScenic(Long page, Long limit) {
        Page<Scenic> page1=new Page<>(page,limit);
        QueryWrapper<Scenic> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("scenic_name");
        Page<Scenic> scenicPage = scenicMapper.selectPage(page1, queryWrapper);
        long total=scenicPage.getTotal();
        List<Scenic> records = scenicPage.getRecords();
        Map<String, Object> map=new HashMap<>();
        map.put("total", total);
        map.put("scenicList",records);
        return map;
    }

    @Override
    public Map<String, Object> getScenicAndImage(Long page, Long limit) {
        Page<Scenic> page1=new Page<>(page,limit);
        QueryWrapper<Scenic> queryWrapper=new QueryWrapper<>();
        queryWrapper.orderByDesc("scenic_name");
        Page<Scenic> scenicPage = scenicMapper.selectPage(page1, queryWrapper);
        long total=scenicPage.getTotal();
        List<Scenic> records = scenicPage.getRecords();
        Map<String, Object> scenicVO = getScenicVO(records);
        scenicVO.put("total", total);
        return scenicVO;
    }


    @Override
    public Map<String, Object> searchScenicByName(String name) {
        QueryWrapper<Scenic> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("scenic_name", name);
        List<Scenic> scenicList = scenicMapper.selectList(queryWrapper);
        Map<String, Object> map=new HashMap<>();
        map.put("scenicList", scenicList);
        return map;
    }

    @Override
    public Map<String, Object> searchScenicAndImage(Long page,Long limit,String name) {
        Page<Scenic> page1=new Page<>(page,limit);
        QueryWrapper<Scenic> queryWrapper=new QueryWrapper<>();
        queryWrapper.like("scenic_name", name);
        return getStringObjectMap(page1, queryWrapper);
    }

    @Override
    public Map<String, Object> searchScenicImageBType(Long page, Long limit, String type) {
        Page<Scenic> page1=new Page<>(page,limit);
        QueryWrapper<Scenic> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("scenic_type",ScenicType.valueOf(type));
        return getStringObjectMap(page1, queryWrapper);
    }

    private Map<String, Object> getStringObjectMap(Page<Scenic> page1, QueryWrapper<Scenic> queryWrapper) {
        Page<Scenic> scenicPage = scenicMapper.selectPage(page1, queryWrapper);
        List<Scenic> records = scenicPage.getRecords();
        Map<String, Object> scenicVO = getScenicVO(records);
        scenicVO.put("total",scenicPage.getTotal());
        return scenicVO;
    }

    private Map<String,Object> getScenicVO(List<Scenic> scenicList){
        List<ScenicVO> scenic=new ArrayList<>();
        scenicList.forEach(record -> {
            Integer scenicId = record.getScenicId();
            QueryWrapper<Image> queryWrapper1=new QueryWrapper<>();
            queryWrapper1.eq("scenic_id", scenicId);
            List<Image> images = imageMapper.selectList(queryWrapper1);
            List<String> url=new ArrayList<>();
            images.forEach(image -> url.add(image.getImageUrl()));
            ScenicVO scenicVO=new ScenicVO(record.getScenicId(),record.getScenicName(),record.getScenicDescription(),record.getScenicType(),record.getIsDeleted(),url);
            scenic.add(scenicVO);
        });
        Map<String, Object> map=new HashMap<>();
        map.put("scenicList", scenic);
        return map;
    }
    @Override
    public Map<String, Object> getScenicDeleted(Long page,Long limit) {
        page=(page-1)*10;
        Integer total = scenicMapper.getCount();
        List<Scenic> records = scenicMapper.getScenicDeleted(page, limit);
        Map<String, Object> map=new HashMap<>();
        map.put("total", total);
        map.put("scenicList",records);
        return map;
    }

    @Override
    public boolean recovery(List<String> ids) {
        int i = scenicMapper.recoverScenicDeleted(ids);
        int i1 = scenicMapper.recoveryImage(ids);
        return i > 0 && i1 > 0;
    }

    @Override
    public void deleteScenic(List<String> ids) {
        ids.forEach(id->{
            List<String> images = scenicMapper.getImageUrl(id);
            images.forEach(image->{
                String fileName = image.substring(42);
                ossUtil.delete(fileName);
            });
        });
        int i = scenicMapper.deleteScenicDeleted(ids);
        int i1 = scenicMapper.deleteImageDeleted(ids);
    }

    @Override
    public void deleteTicket() {
        QueryWrapper<ScenicTicket> queryWrapper=new QueryWrapper<>();
        long time = new Date().getTime();
        queryWrapper.le("scenic_ticket_expire", new Date(time-86400*1000*10L));
        ticketMapper.delete(queryWrapper);
    }
}
